<template>
    <div>
        <div class="addFriendCon">
            <div class="addFriendImg"><img :src="searchFriend.url"></div>
            <div class="addFriendLine" >{{note}}</div>
            <div class="addFriendLine2">芸券号：{{searchFriend.id}}</div>
        </div>
        <div class="addFriendBtn" @click="addFriend" v-if="addFriendShow">添加券友</div>
        <div class="addFriendBtn" @click="shareCoupon">分享电子券</div>
    </div>
</template>
<script>
    import * as types from '../../vuex/types.js'
    import { mapState } from 'vuex'
    import getData from '../../service/getData.js'
    import getData2 from '../../service/getData2.js'
    import { Toast } from 'mint-ui';
    export default {
        data:() => ({
            addFriendShow:false,
            note:'',
            friendInfo:[],
        }),
        computed:{
            ...mapState({
                searchFriend:state => state.center.searchFriend,
                    userInfo:state => state.user.userInfo
            })
        },
        methods:{
            addFriend:function(){
                let me = this;
                getData.addFriend({method:'post',data:{friendId:me.searchFriend.id}},me.userInfo.usrId).then( rep => {
                    console.info(JSON.stringify(rep));
                    if(rep.respCode == '00'){
                        Toast({
                            message: '发送成功',
                            duration: 1000
                        });
                        setTimeout(function(){
                            // me.$router.push({"path":'/friends'});
                            window.history.go(-2);
                        },1200)
                    }else{
                        Toast({
                            message: rep.errorInfo,
                            duration: 1000
                        });
                    }
                })
            },
            //分享电子券
            shareCoupon(){
                this.$store.commit("shareCoupUser", {
                    friendId: this.searchFriend.id
                });
                this.$router.push({
                    path: '/shareList'
                });
            }
        },
        async created(){
            //请求好友列表，确认是否已是好友
            let rep = await getData.myFriends({method:"post"},this.userInfo.usrId);
            if(rep.respCode == '00'){
                console.log(rep);
                this.addFriendShow =  !rep.content.friends.some(data => data.friendId === this.searchFriend.id);
                //若搜索的人已是好友，取出其信息
                this.friendInfo = rep.content.friends.filter(data => data.friendId === this.searchFriend.id);
                for(let i=0;i<this.friendInfo.length;i++){
                    if(this.friendInfo[0].friendNote){
                        this.note = this.friendInfo[0].friendNote
                    }else{
                        this.note = this.searchFriend.name
                    }
                }
            }
        }
    }
</script>
